﻿<%@ Page Language="C#" AutoEventWireup="true" Inherits="System.Web.UI.Page" %>

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Portal.Core.Helper" %>
<%@ Import Namespace="Portal.Data.Entity" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Image upload</title>
</head>
<body>
    <form id="form1" runat="server">
        <div runat="server" id="container" visible="False">
            <h1>
                <asp:Literal ID="bookName" runat="server"></asp:Literal></h1>
            <asp:HiddenField runat="server" ID="hidBid" />
            <asp:HiddenField runat="server" ID="hidImg" />
            <asp:Literal ID="ltrLink" runat="server"></asp:Literal><br />
            <table>
                <tr valign="top">
                    <td style="border: solid 1px #ccc;padding: 5px;">
                        <h3>Tải ảnh từ máy tính:</h3>
                        <asp:FileUpload ID="fu" runat="server" /><br/><br/>
                        <asp:Button ID="Save" runat="server" Text="Upload" OnClick="Save_Click" />
                    </td>
                    <td style="border: solid 1px #ccc;padding: 5px;border-left: none;width: 60%">
                        <h3>Lấy link từ website khác:</h3>
                        <asp:TextBox ID="txtImgUrl" runat="server" Width="99%"></asp:TextBox>
                        <br/><br/>
                        <asp:Button ID="btnSetImageUrl" runat="server" Text="Chấp nhận" OnClick="OnbtnSetImageUrlClick" />
                    </td>
                </tr>

            </table>


            <input id="btnClose" type="button" value="Đóng lại" style="float: right;margin-right: 3px;"/>
        </div>
        <div runat="server" id="noEntry" visible="True">
            <h2>Hãy chọn Book cụ thể!</h2>
        </div>
    </form>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script>
        jQuery(document).ready(function ($) {
            $("#btnClose").click(function () {
                var params = $('#<%=hidBid.ClientID%>').val() + "|" + $('#<%=hidImg.ClientID%>').val();
                var radWindow = GetRadWindow();
                radWindow.argument = params;
                radWindow.close();
            });
        });
        function GetRadWindow() {
            var oWindow = null;
            if (window.radWindow) oWindow = window.radWindow;
            else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
            return oWindow;
        }
    </script>
</body>
</html>
<script runat="server">
    private int bid;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Portal.Data.PortalContext.CurrentUser == null)
        {
            form1.Visible = false;
            return;
        }

        int.TryParse(Request["bid"], out bid);
        if (bid > 0)
        {
            var b = Book.FindByPrimaryKey(bid);
            if (b != null)
            {
                container.Visible = true;
                noEntry.Visible = false;
                hidBid.Value = b.ID.ToString();
                bookName.Text = b.FullName;
                SetImage(b.Image);
            }
        }
    }
    void SetImage(string img)
    {
        var thumb = Book.RenderImage(img, 50);
        if (thumb.Length>0)
        {
            ltrLink.Text = string.Format("<img src='{0}'/><br/>{0}", thumb);
            hidImg.Value = thumb;
        }
    }

    private void Save_Click(object sender, EventArgs e)
    {
        if (fu.HasFile)
        {
            var b = Book.FindByPrimaryKey(bid);
            if (b != null)
            {
                var savePath = "files/book/";
                var physicalPath = FileHelper.GetPhysicalPath(savePath);
                if (!Directory.Exists(physicalPath))
                    Directory.CreateDirectory(physicalPath);

                savePath += (string.IsNullOrEmpty(b.SortName)) ? b.ID.ToString() : b.SortName;
                string fname = fu.PostedFile.FileName;
                savePath += bid + fname.Substring(fname.IndexOf("."), fname.Length - fname.IndexOf("."));
                fu.SaveAs(FileHelper.GetPhysicalPath(savePath));

                b.Image = savePath;

                //upload to picasa
                var bloggerAcc = GoogleHelper.GetBloggerAccount();
                try
                {
                    var gInfos = GoogleHelper.UploadImageToGoogle(FileHelper.GetPhysicalPath(b.Image), b.SortName, bloggerAcc.UserName, bloggerAcc.Password, "truyengicungco");
                    if (gInfos.Count > 0)
                    {
                        var googleImage = gInfos["url"].ToString();
                        //remove image temps here...
                        FileHelper.Remove(b.Image);
                        //set new full google path
                        b.Image = googleImage;
                    }
                }
                catch { }
                b.UpdateOnly();

                SetImage(b.Image);
            }
        }
    }

    private void OnbtnSetImageUrlClick(object sender, EventArgs e)
    {
        var newImg = txtImgUrl.Text.Trim();
        var b = Book.FindByPrimaryKey(bid);
        if (b != null && newImg.Length>0)
        {
            b.Image = newImg;
            b.UpdateOnly();
            SetImage(b.Image);
        }
    }

</script>
